﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace DScheme.DancerClasses
{
    /// <summary>вспомогательный класс для задачи действий танцорам</summary>
    class DancerMover
    {
        public double NextStartTime = 0;
        public double PrevTime = 0;

        Dancer dancer;

        public DancerMover(Dancer dancer)
        {
            this.dancer = dancer;
        }

        public void TimeTo(double weightTime)
        {
            PrevTime = NextStartTime;
            NextStartTime += weightTime;
        }

        public void LineTo(Point newPos, double moveTime)
        {
            LineTo(newPos, moveTime, true);
        }

        public void LineTo(Point newPos, double moveTime, bool goToTime)
        {
            dancer.al.Add(new DancerActionMoveTo(dancer, newPos, NextStartTime, moveTime));
            if (goToTime)
            {
                PrevTime = NextStartTime;
                NextStartTime += moveTime;
            }
        }

        public void RotateOn(double moveAngle, double moveTime)
        {
            RotateOn(moveAngle, moveTime, true);
        }

        public void RotateOn(double moveAngle, double moveTime, bool goToTime)
        {
            dancer.al.Add(new DancerActionRotateOn(dancer, moveAngle, NextStartTime, moveTime));
            if (goToTime)
            {
                PrevTime = NextStartTime;
                NextStartTime += moveTime;
            }
        }

        public void RotateTo(double moveAngle, double moveTime)
        {
            RotateTo(moveAngle, moveTime, true, false);
        }

        public void RotateTo(double moveAngle, double moveTime, bool goToTime, bool rotateByShortWay)
        {
            dancer.al.Add(new DancerActionRotateTo(dancer, moveAngle, NextStartTime, moveTime, rotateByShortWay));
            if (goToTime)
            {
                PrevTime = NextStartTime;
                NextStartTime += moveTime;
            }
        }

        public void LineOn(int moveX, int moveY, double moveTime, bool goToTime)
        {
            dancer.al.Add(new DancerActionMoveOn(dancer, moveX, moveY, NextStartTime, moveTime));
            if (goToTime)
            {
                PrevTime = NextStartTime;
                NextStartTime += moveTime;
            }
        }

        public void CircleTo(double radius, double startAngle, bool backward, double moveAngle, double moveTime)
        {
            CircleTo(radius, startAngle, backward, moveAngle, moveTime, true);
        }

        public void CircleTo(double radius, double startAngle, bool backward, double moveAngle, double moveTime, bool goToTime)
        {
            dancer.al.Add(new DancerActionMoveByCircleTo(dancer, radius, startAngle, moveAngle, backward, NextStartTime, moveTime));
            if (goToTime)
            {
                PrevTime = NextStartTime;
                NextStartTime += moveTime;
            }
        }

        public void EllipseTo(double radius1, double radius2, double startAngle, bool backward, double moveAngle, double moveTime)
        {
            EllipseTo(radius1, radius2, startAngle, backward, moveAngle, moveTime, true);
        }

        public void EllipseTo(double radius1, double radius2, double startAngle, bool backward, double moveAngle, double moveTime, bool goToTime)
        {
            dancer.al.Add(new DancerActionMoveByEllipseOn(dancer, radius1, radius2, startAngle, moveAngle, backward, NextStartTime, moveTime));
            if (goToTime)
            {
                PrevTime = NextStartTime;
                NextStartTime += moveTime;
            }
        }

        public void NormalizeAngle()
        {
            dancer.al.Add(new DancerActionNormalizeAngle(dancer, NextStartTime));
        }

        /// <summary>проводит угол к значению от 0 до 359.(9)</summary>
        /// <param name="angle">исходный угол</param>
        public static double NormalizeAngle(double angle)
        {
            while (angle < 0) angle += 360;
            while (angle >= 360) angle -= 360;
            return angle;
        }

    }
}
